package com.jvyou.jdbc;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;

/**
 * @author 橘柚
 * @version 1.0-SNAPSHOT
 * @Date 2024/7/17 14:57
 * @Description
 */
public class BaseDemo {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.加载MySQL驱动
//        DriverManager.registerDriver(new Driver());
        // 2.获取数据库连接
        String url = "jdbc:mysql://localhost:3306/jvyou-mybatis";
        Connection connection = DriverManager.getConnection(url, "root", "123456");
        // 3.获取 预编译陈述
        PreparedStatement ps = connection.prepareStatement("select * from user where id =?");
        // 4.设置参数
        ps.setInt(1, 1);
        // 5。执行查询
        ps.execute();
        // 6.处理结果集
        ResultSet resultSet = ps.getResultSet();
        while (resultSet.next()) {
            System.out.println(resultSet.getLong("id"));
            System.out.println(resultSet.getString("name"));
        }
        // 7.关闭资源(先开后关原则)
        resultSet.close();
        ps.close();
        connection.close();
    }

}
